package com.wfm.leetcode.editor.cn;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.Test;

/**
 * a b 两个线程执行任务，谁先执行完，main线程立马返回
 */
public class P多线程任务执行完立马返回最小时间线程 {
    public static void main(String[] args) throws InterruptedException {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            return a1();
        });
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            return a2();
        });

        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(future1, future2);
        objectCompletableFuture.thenAccept((s)->{
            System.out.println(s);
        });

        Thread.sleep(10000);

    }
    private static int a1() {
        double random = Math.random();
        int i = (int) (random * 10);
        System.out.println("a1"+i);
        try {
            Thread.sleep(i* 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return (int)(random*10);
    }
    private static int a2() {
        double random = Math.random();
        int i = (int) (random * 10);
        System.out.println("a2"+i);
        try {
            Thread.sleep(i* 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return (int)(random*10);

    }
    @Test
    public void test(){
        double random = Math.random();
        System.out.println((int)(random*10));
    }

}
